package com.woniuxy.main.mycenter_ty.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.main.mycenter_ty.pojo.Follow;
import com.woniuxy.main.mycenter_ty.pojo.User_Ty;
import com.woniuxy.main.mycenter_ty.service.MyCenterService;

import lombok.Data;

/**
 *  个人资料页面
 * @author 
 */
@Controller
@Data
public class MyCenterHandler {
	@Autowired
	private MyCenterService personCenterservice;

	
	/**
	 *  查询当前用户的头像，昵称，关注,粉丝数
	 * @return
	 */
	@RequestMapping("/personcenter")
	public ModelAndView personCenter() {
		// 获取当前登录的账号
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		User_Ty user = personCenterservice.queryAllInfoByPhone(phone); // 个人资料
		Integer myFollow = personCenterservice.myFollow(user.getUser_id());// 关注数
		Integer followMe = personCenterservice.followMe(user.getUser_id());// 粉丝数
		ModelAndView mo = new ModelAndView();
		mo.setViewName("/personcenter.html");
		mo.addObject("user", user);
		mo.addObject("myFollow", myFollow);
		mo.addObject("followMe", followMe);
		return mo;
	}
	

	/**
	 * 查询当前用户的详细资料
	 * @return
	 */
	@RequestMapping("/user_info")
	public ModelAndView userInfo() {
		// 获取当前登录的账号
		String account = (String) SecurityUtils.getSubject().getPrincipal();
		User_Ty user = personCenterservice.queryAllInfoByPhone(account); // 个人资料
		ModelAndView mo = new ModelAndView();
		mo.setViewName("/user_info.html");
		mo.addObject("user", user);
		return mo;
	}

	
	/**
	 *  更改个人资料
	 * @param nickname
	 * @param sex
	 * @param age
	 * @param name
	 * @param adress
	 * @param qq
	 * @return
	 */
	@RequestMapping("/updateInfo")
	@ResponseBody
	public String updateInfo(String nickname, String sex, Integer age, String name, String adress, String qq) {
		// 获取当前登录的账号
		User_Ty user = new User_Ty();
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		user.setUser_nickname(nickname);
		user.setUser_sex(sex);
		user.setUser_age(age);
		user.setUser_name(name);
		user.setAdress(adress);
		user.setQq(qq);
		user.setUser_phone(phone);
		personCenterservice.updateInfo(user); // 个人资料
		return "/personcenter.html";
	}
	

	/**
	 *  获取所有粉丝具体信息
	 * @return   List<Follow>粉丝列表
	 */
	@RequestMapping("/fansList")
	@ResponseBody
	public List<Follow> fansList() {
		// 获取当前登录的账号
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		int id = personCenterservice.queryAllInfoByPhone(phone).getUser_id(); // 个人id
		List<Follow> fanList = personCenterservice.queryFans(id); // 粉丝资料列表
		return fanList;
	}
	

	/**
	 * 获取自己所有已关注具体信息
	 * @return List<Follow> 关注列表
	 */
	@RequestMapping("/followList")
	@ResponseBody
	public List<Follow> FollowList() {
		// 获取当前登录的账号
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		int id = personCenterservice.queryAllInfoByPhone(phone).getUser_id(); // 本人id
		List<Follow> followList = personCenterservice.queryFollow(id); // 本人已关注资料列表
		return followList;
	}

	
	/**
	 * 关注自己的粉丝 ----> 变为好友
	 * @param fanId: 粉丝的id
	 * @param userId: 被关注id
	 * @return
	 */
	@RequestMapping("/addFirend")
	@ResponseBody
	public String addFirend(int fanId, int userId) {
		Follow fan = new Follow();
		fan.setUser_id(fanId); // 粉丝的id
		fan.setFollowed_id(userId); // 被关注者的id
		personCenterservice.addFirend(fan);
		return "互相关注成功，已成为好友";
	}

	
	/**
	 *  取消自己已关注（软删除）
	 * @param   fanId:  自己的id 
	 * @param  userId：被自己关注的id
	 * @return
	 */
	@RequestMapping("/delFollow")
	@ResponseBody
	public String delFollow(int fanId, int userId) {
		Follow fan = new Follow();
		fan.setUser_id(fanId); // 自己的id
		fan.setFollowed_id(userId); // 被自己关注的id
		personCenterservice.delFollow(fan);		
		return "成功删除已关注";
	}
		
	
	 /**
	  * 获取当前被点击用户的详细资料
	  * @param uid
	  * @return
	  */	
	@RequestMapping("/someOnePersonInfo")
	public ModelAndView someOnePersonInfo(int uid) {
		User_Ty user = personCenterservice.queryAllInfoByUser_id(uid);  //查询指定user_id的用户详细资料
		ModelAndView mo = new ModelAndView();
		mo.setViewName("/someOnePersonInfo.html");
		mo.addObject("user", user);
		return mo;
	}
	
	
	

	// 教练认证
	@RequestMapping("/coachApprove")
//	@ResponseBody
	public ModelAndView coachApprove() {
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		User_Ty user = personCenterservice.queryAllInfoByPhone(phone); // 个人资料
		Integer userId=user.getUser_id();   // 当前用户的id
		System.out.println("账号"+userId);
		
		Integer approveState = personCenterservice.sreachApprove(userId);  //查询指定user_id的用户详细资料
		ModelAndView mo = new ModelAndView();
		mo.setViewName("coach-approve");
		mo.addObject("approveState", approveState);
		return mo;
	}
	
	//修改认证状态
	@RequestMapping("/upApprove")
	@ResponseBody
	public Map upApprove() {
		String phone = (String) SecurityUtils.getSubject().getPrincipal();
		User_Ty user = personCenterservice.queryAllInfoByPhone(phone); // 个人资料
		Integer userId=user.getUser_id();   // 当前用户的id
		
		String reslut = personCenterservice.upApprove(userId);  //查询指定user_id的用户详细资料
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("reslutApprove", reslut);
		return map;
	}
	// 绑定手机

}
